#!/bin/sh

#--------------------------------------------------------------------
# Git Utilities
#--------------------------------------------------------------------

case "$fn" in

    # Display local Git configuration
    "brew:git:config")

        if [ "$echocommand" == "true" ]; then
            echo "${GREEN}git config --list\n\n${NC}"
        fi
        git config --list
    ;;


    # Open current Git repository on Github
    "git:open")

        giturl=$(git config --get remote.origin.url)
          if [ "$giturl" == "" ]
            then
             echo "Not a git repository or no remote.origin.url set"
             exit 1;
          fi
          giturl=${giturl/git\@github\.com\:/https://github.com/}
          giturl=${giturl/\.git/\/}
          branch="$(git symbolic-ref HEAD 2>/dev/null)" ||
          branch="(unnamed branch)"     # detached HEAD
          branch=${branch##refs/heads/}
          giturl=$giturl
          open $giturl
    ;;


    # Create Git branch based on current branch
    "git:create:branch")

        if [ ! -z "$firstParameter" -a "$firstParameter" != " " ]; then
            if [ "$echocommand" == "true" ]; then
                echo "${GREEN}git checkout -b ${GRAY}$firstParameter\n\n${NC}"
            fi
            git checkout -b $firstParameter

        else
            echo "Please specify the name for the new branch"
            echo "E.g.: git:create:branch sample-branch"
        fi
    ;;


    # Get last update date for all branches in current project
    "git:branches")

        git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
    ;;


    # Undo latest commit
    "git:undo-commit")

        if [ "$echocommand" == "true" ]; then
            echo "${GREEN}git reset --soft HEAD~\n\n${NC}"
        fi
        git reset --soft HEAD~
    ;;


    # See latest commits IDs and titles for current branch
    "git:log")

        if [ "$echocommand" == "true" ]; then
            echo "${GREEN}git log --pretty=oneline\n\n${NC}"
        fi
    	git log --pretty=oneline
    ;;


    # See all branches
    "git:branch")

        if [ "$echocommand" == "true" ]; then
            echo "${GREEN}git branch -a\n\n${NC}"
        fi
        git branch -a
    ;;


    # Rename Git branch
    "git:branch:rename")

         if [ ! -z "$firstParameter" -a "$firstParameter" != " " ]; then
            if [ "$echocommand" == "true" ]; then
                echo "${GREEN}git branch -m ${GRAY}$firstParameter\n\n${NC}"
            fi
            git branch -m $firstParameter

        else
            echo "Please specify the name of the branch to rename"
            echo "E.g.: git:branch:rename sample-branch"
        fi
    ;;


    # Remove local Git branch
    "git:branch:remove-local")

         if [ ! -z "$firstParameter" -a "$firstParameter" != " " ]; then
            if [ "$echocommand" == "true" ]; then
                echo "${GREEN}git branch -d ${GRAY}$firstParameter\n\n${NC}"
            fi
            git branch -d $firstParameter

        else
            echo "Please specify the name of the branch to remove"
            echo "E.g.: git:branch:remove-local sample-branch"
        fi
    ;;


    # Remove local and remote Git branch
    "git:branch:remove-remote")

         if [ ! -z "$firstParameter" -a "$firstParameter" != " " ]; then
            if [ "$echocommand" == "true" ]; then
                echo "${GREEN}git branch -d ${GRAY}$firstParameter\n\n${NC}"
            fi
            git branch -d $firstParameter
            if [ "$echocommand" == "true" ]; then
                echo "${GREEN}git push origin --delete ${GRAY}$firstParameter\n\n${NC}"
            fi
            git push origin --delete $firstParameter

        else
            echo "Please specify the name of the branch to remove"
            echo "E.g.: git:branch:remove-remote sample-branch"
        fi
    ;;


    # Check Git settings
    "git:config")

        if [ "$echocommand" == "true" ]; then
            echo "${GREEN}git config --list\n\n${NC}"
        fi
        git config --list
    ;;


    # Add removed files to staged files
    "git:add-removed")

        if [ "$echocommand" == "true" ]; then
            echo "${GREEN}git add -u\n\n${NC}"
        fi
        git add -u
    ;;


    # Open current Git repository URL
    "git:open")

        open `git remote -v | awk '/fetch/{print $2}' | sed -Ee 's#(git@|git://)#http://#' -e 's@com:@com/@'`| head -n1
    ;;


    # Get size of current Git repository
    "git:size")

        git bundle create tmp.bundle --all
        echo "\n\nCurrent Git repository size: "
        du -sh tmp.bundle
        rm tmp.bundle
    ;;


esac